package com.damon;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.damon.entity.ApiEntity;
import com.damon.log.WcsLog;
import com.damon.thread.ApiThread;
import com.damon.utils.DbUtil;
import com.damon.utils.GlobalOjbectUtil;
import com.damon.utils.LogUtil;
import com.damon.utils.ThreadPoolUtil;
import com.damon.window.WcsWindow;

public class WcsHttpClientRun {
//	static Logger logger = LoggerFactory.getLogger(WcsPrinterRun.class);
//	public static Map<String, Queue<String>> addressMap = new HashMap<>();
	public static void main(String[] args) {
		//实例化可视化界面
		WcsWindow wcsWindow = new WcsWindow();
		GlobalOjbectUtil.setWindow(wcsWindow);
		//加载日志
		WcsLog logger = LogUtil.getLogger(WcsHttpClientRun.class);
		logger.info("正在检测需要调用的接口。。。。。。。。");
		String sql = "select ID,HTTP_SYS,HTTP_URL_DESC,PROC_NAME,PROC_NAME_CONFIRM,HTTP_INTERVAL,HTTP_URL,HTTP_TYPE from WMS_HTTP_CLIENT where ISOK=1";
		Connection connection = DbUtil.getConnection();
		Statement statement = null;
		ResultSet resultSet;
		try {
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while(resultSet.next()) {
				ApiEntity apiEntity = new ApiEntity();
				apiEntity.setId(resultSet.getInt("ID"));
				apiEntity.setHttpSys(resultSet.getString("HTTP_SYS"));
				apiEntity.setHttpUrlDesc(resultSet.getString("HTTP_URL_DESC"));
				apiEntity.setProcName(resultSet.getString("PROC_NAME"));
				apiEntity.setProcNameConfirm(resultSet.getString("PROC_NAME_CONFIRM"));
				apiEntity.setHttpInterval(resultSet.getString("HTTP_INTERVAL"));
				apiEntity.setHttpUrl(resultSet.getString("HTTP_URL"));
				apiEntity.setHttpType(resultSet.getString("HTTP_TYPE"));
				GlobalOjbectUtil.getApiList().add(apiEntity);
			}
		} catch (SQLException e) {
			logger.error("查询待调用接口失败！！");
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		//为每个待调用的接口创建线程
		logger.info("已检测到："+GlobalOjbectUtil.getApiList().size()+" 个待回传的接口，开始创建线程！");
		for(ApiEntity apiInfo : GlobalOjbectUtil.getApiList()) {
			ThreadPoolUtil.getExecutor().execute(new ApiThread(apiInfo));
		}
	}
	
}
